package cn.zlp.hrm.controller;

import cn.zlp.hrm.service.IOrderService;
import cn.zlp.hrm.domain.Order;
import cn.zlp.hrm.query.OrderQuery;
import cn.zlp.hrm.util.AjaxResult;
import cn.zlp.hrm.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    public IOrderService orderService;


    /**
     * 保存和修改公用的
     * @param order 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Order order) {
        try {
            if ( order.getId() != null){
                    orderService.updateById(order);
            }
            else{
                    orderService.insert(order);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
                orderService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Order get(@PathVariable("id") Long id) {
        return orderService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Order> list() {

        return orderService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Order> json(@RequestBody OrderQuery query) {
        Page<Order> page = new Page<Order>(query.getPage(), query.getRows());
        page = orderService.selectPage(page);
        return new PageList<Order>(page.getTotal(), page.getRecords());
    }

    /**
     * 参数：预创订单sn，支付方式
     * @param params 支付包数据
     * @return
     */
    @PutMapping("/kill")
    public AjaxResult killOrder(@RequestBody Map<String,String> params){

        return orderService.killOrder(params);

    }
}
